package com.example.hao.animation_demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class View5Activity extends AppCompatActivity {

    private ImageView pig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view5);
        initView();
    }

    private int value;

    private void initView() {
        pig = findViewById(R.id.pig);
        AnimationDrawable drawable = (AnimationDrawable) pig.getDrawable();
        drawable.start();
        final ValueAnimator anim = ValueAnimator.ofInt(1, 800);
        anim.setDuration(2000);
        anim.setRepeatCount(1);
        anim.setRepeatMode(ValueAnimator.REVERSE);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                value = (int) animation.getAnimatedValue();
                pig.setX(value);
            }
        });
        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
                pig.setRotationY(180);
            }
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                anim.start();
                pig.setRotationY(360);
            }
        });
    }
}
